<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="com.googlecode.contests_repo.core.users.AccessRight"%>
<%@page import="com.googlecode.contests_repo.core.config.ConfigurationManager"%>
<%@page import="com.googlecode.contests_repo.core.users.UserManager"%>
<%@page import="com.googlecode.contests_repo.core.model.*"%>
<%@page import="java.io.File"%>
<%@page import="java.util.List"%>

	<%   
		if (request.getParameter("submittedSeries") != null) {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/SeriesEditServlet");
			dispatcher.include(request, response);
		}

		String seriesId = request.getParameter("path"); //request.getParameter("series");
		String repoPath = ConfigurationManager.getInstance().getRepoRoot();
		String seriesPath = repoPath + File.separator + seriesId;
		Series series = ResourcesUtils.getJSON(new File(seriesPath));
		if (series == null) {
	%>
	<p>The requested series does not exist.</p>
	<%
		}
		else {
			String seriesRelativePath = ConfigurationManager.getInstance().getSeriesRelativePath();
			String publicFolder = "_public_files";
			String privateFolder = "_files";
			String loggedUser = (String) session.getAttribute("username");
			
			if (!UserManager.hasAccessRight(loggedUser, seriesPath, AccessRight.VIEW)) {
	%>
		<p>You don't have permission to view this series.</p>
	<%
			}
			else {
				
			boolean canEdit = "1".equals(request.getParameter("edit"));
			boolean hasUserEditRights = UserManager.hasAccessRight(loggedUser, seriesPath, AccessRight.VIEW_FULL);
			String readOnly = (!canEdit || !hasUserEditRights) ? "readonly=\"readonly\"" : "";
	%>
	<h1>Series Info</h1>

	<form method="post" action="">
		<p>
			<b>Id: </b> <input type="text" name="id"
				readonly="readonly" value="<%=series.getId()%>" />
		</p>
		<p>
			<b>Title: </b> <input type="text"
				name="title" <%=readOnly%> value="<%=series.getTitle()%>" /> <input
				type="hidden" name="old-title" value="<%=series.getTitle()%>" />
		</p>
		<p>
			<b>About: </b> <input type="text"
				name="about" <%=readOnly%> value="<%=series.getAbout()%>" /> <input
				type="hidden" name="old-about" value="<%=series.getAbout()%>" />
		</p>
		<%
			if (hasUserEditRights) {
		%>
		<p>
				<b>Notes: </b> <input type="text"
				name="notes" <%=readOnly%> value="<%=series.getNotes()%>" /> <input
				type="hidden" name="old-notes" value="<%=series.getNotes()%>" />
		</p>
			<%
					if (canEdit) {
			%>
		<p>
			<input type="submit" name="submittedSeries" value="Change" />
			<a href="resource.jsp?path=<%=seriesId%>">Discard changes</a>
		</p>
			<%
					}
				}
			%>
		
	</form>

	<h2>Contests:</h2>
	<table>
		<tr>
			<th>Id</th>
			<th>Title</th>
			<th>About</th>
			<th>View</th>
			<th>Download</th>
			<%
				if (canEdit) {
			%>
			<th>Edit</th>
			<%
				}
				if (canEdit && hasUserEditRights) {
			%>
			<th>Delete</th>
			<%
				}
			%>
		</tr>
		<%
			List<Contest> contestList = ResourcesUtils.fetchResources(new File(
					seriesPath));
			if (contestList != null) {
				for (Contest contest : contestList) {
					String contestDir = seriesPath + File.separator
							+ contest.getId();
					if (UserManager.hasAccessRight(loggedUser, contestDir, AccessRight.LIST)) {
		%>
		<tr>
			<td><%=contest.getId()%></td>
			<td><%=contest.getTitle()%></td>
		<%
					}
					if (UserManager.hasAccessRight(loggedUser, contestDir, AccessRight.VIEW)) {
		%>
			<td><%=contest.getAbout()%></td>
			<td><a href="resource.jsp?path=<%=seriesId + File.separator + contest.getId()%>">details</a></td>
			<td><a href="fetchZipped?contest=<%=contest.getId()%>&amp;series=<%=seriesId%>">download</a></td>
			<% 
					}
					if (canEdit && UserManager.hasAccessRight(loggedUser, contestDir, AccessRight.VIEW_FULL)) {
			%>
			<td><a
				href="resource.jsp?path=<%=String.format("%s%s%s", seriesId, File.separator,
								contest.getId())%>&amp;edit=1">edit</a>
			</td>
			<%
					}
					if (canEdit && hasUserEditRights) {
			%>
			<td><a href="delete_file?path=<%=String.format("%s%s%s", seriesId, File.separator,
			contest.getId()) %>">delete</a></td>
			<%
					}
			%>
		</tr>
		<%
				}
			}
			}
		}
		%>
	</table>
